package com.digdes.calendar.gwt.server;

import com.digdes.calendar.gwt.client.dto.EventDTOImpl;
import com.digdes.calendar.gwt.client.dto.PlaceDTOImpl;
import com.digdes.calendar.gwt.client.dto.UserDTOImpl;
import com.digdes.calendar.gwt.client.service.CalendarService;
import com.digdes.calendar.model.Place;
import com.digdes.calendar.model.User;
import com.digdes.calendar.model.impl.PlaceImpl;
import com.digdes.calendar.model.impl.UserImpl;
import com.digdes.calendar.service.EventService;
import com.digdes.calendar.service.PlaceService;
import com.digdes.calendar.service.UserService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class CalendarServiceImpl extends RemoteServiceServlet implements CalendarService {

    private EventService eventService;
    private ServletContext servletContext;

    public void deleteEvent(Long id) {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");
        eventService.deleteEvent(id);
    }
    public ArrayList<EventDTOImpl> getOldEvents(String d) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");
        List<EventDTOImpl> events = new ArrayList<EventDTOImpl>();
        try {
            events = eventService.getOldEventsByDate(simpleDateFormat.parse(d));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (ArrayList<EventDTOImpl>) events;
    }

    public ArrayList<EventDTOImpl> getEvents(String d) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");
        List<EventDTOImpl> events = new ArrayList<EventDTOImpl>();
        try {
            events = eventService.getEventsByDate(simpleDateFormat.parse(d));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (ArrayList<EventDTOImpl>) events;
    }
    public ArrayList<EventDTOImpl> getEventsBetween(String dStart, String dFinish) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");
        List events = null;
        try {
            events = eventService.getEventsBetween(simpleDateFormat.parse(dStart),
                    simpleDateFormat.parse(dFinish));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<EventDTOImpl>(events);
    }

   public EventDTOImpl saveEvent(EventDTOImpl eventDTO) {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");

        try {
            eventService.saveEvent(eventDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return eventDTO;
    }

    public Set<UserDTOImpl> getUsers() {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserService userService = (UserService) applicationContext.getBean("userService");
        return userService.getAllUser();
    }

    public Set<EventDTOImpl> getEventByPlaceAndDate(Long id, Date start, Date finish) {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");
        return eventService.getEventsByPlaceAndDate(id, start, finish);
        //
    }

    public Set<EventDTOImpl> getEventByUserAndDate(Long id, Date start, Date finish) {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        EventService eventService = (EventService) applicationContext.getBean("eventService");
        return eventService.getEventsByUserAndDate(id, start, finish);
    }

    public Set<PlaceDTOImpl> getAllPlace() {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        PlaceService placeService = (PlaceService) applicationContext.getBean("placeService");
        return placeService.getAllPlace();
    }

    public String addPlace(PlaceDTOImpl placeDTO){
        String a;
        a = null;

        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        PlaceService placeService = (PlaceService) applicationContext.getBean("placeService");
        Place place = new PlaceImpl();
        place.setAddress(placeDTO.getAddress());
        
        try{
        placeService.savePlace(place);
        }       catch (Exception e){
            e.printStackTrace();
        }
        
        
        
        
        return a;
    }
    
    
    public Long addUser(UserDTOImpl userDTO){
        Long i ;
        i = null;

        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserService userServices = (UserService) applicationContext.getBean("userService");
        User user = new UserImpl();
        user.setEmail(userDTO.getEmail());
        user.setName(userDTO.getName());
        user.setPost(userDTO.getPost());
        user.setSurname(userDTO.getSurname());
        user.setPatronymic(userDTO.getPatronymic());

        try{
            userServices.saveUser(user);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
}